VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form DLGexport 
   BackColor       =   &H00E0E0E0&
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Export"
   ClientHeight    =   5985
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4065
   BeginProperty Font 
      Name            =   "Verdana"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "FRMexport.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   5985
   ScaleWidth      =   4065
   ShowInTaskbar   =   0   'False
   StartUpPosition =   1  'CenterOwner
   Begin MSComctlLib.ProgressBar ProgressBar 
      Height          =   255
      Left            =   45
      TabIndex        =   10
      Top             =   5040
      Visible         =   0   'False
      Width           =   3975
      _ExtentX        =   7011
      _ExtentY        =   450
      _Version        =   393216
      Appearance      =   1
      Scrolling       =   1
   End
   Begin VB.CommandButton CMDbrowse 
      BackColor       =   &H00E0E0E0&
      Caption         =   "Browse"
      Height          =   375
      Left            =   2880
      TabIndex        =   9
      Top             =   720
      Width           =   1095
   End
   Begin VB.Frame Frame1 
      BackColor       =   &H00E0E0E0&
      Caption         =   "Export Type"
      BeginProperty Font 
         Name            =   "Verdana"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   615
      Left            =   45
      TabIndex        =   6
      Top             =   4200
      Width           =   3975
      Begin VB.OptionButton RADIOexportToText 
         BackColor       =   &H00E0E0E0&
         Caption         =   "Export To Text File"
         Height          =   255
         Left            =   1920
         TabIndex        =   8
         Top             =   270
         Value           =   -1  'True
         Width           =   1935
      End
      Begin VB.OptionButton RADIOexportToExcel 
         BackColor       =   &H00E0E0E0&
         Caption         =   "Export To Excel"
         Height          =   255
         Left            =   120
         TabIndex        =   7
         Top             =   270
         Width           =   1695
      End
   End
   Begin VB.CommandButton CMDcancel 
      Caption         =   "Cancel"
      Height          =   375
      Left            =   3000
      TabIndex        =   5
      Top             =   5520
      Width           =   975
   End
   Begin VB.TextBox TXTexportPath 
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   177
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   285
      Left            =   45
      MaxLength       =   255
      TabIndex        =   3
      Top             =   360
      Width           =   3975
   End
   Begin VB.ListBox LISTfields 
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   177
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   2595
      ItemData        =   "FRMexport.frx":0442
      Left            =   45
      List            =   "FRMexport.frx":0444
      MultiSelect     =   2  'Extended
      Sorted          =   -1  'True
      TabIndex        =   1
      Top             =   1440
      Width           =   3975
   End
   Begin VB.CommandButton CMDExport 
      Caption         =   "Export"
      Height          =   375
      Left            =   1560
      TabIndex        =   0
      Top             =   5520
      Width           =   1335
   End
   Begin VB.Label Label2 
      BackStyle       =   0  'Transparent
      Caption         =   "Export As"
      Height          =   255
      Left            =   60
      TabIndex        =   4
      Top             =   150
      Width           =   1095
   End
   Begin VB.Label Label1 
      BackStyle       =   0  'Transparent
      Caption         =   "Fields To Export"
      Height          =   255
      Left            =   60
      TabIndex        =   2
      Top             =   1200
      Width           =   1335
   End
End
Attribute VB_Name = "DLGexport"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private sFilePath As String

Dim i As Integer
Dim j As Integer

' Form load event ----------------------------------------------------------------------------------------------------------------------

Private Sub Form_Load()

    initForm

End Sub

' Initialize the form. Set the load the fields list, with the fields in the main form's listbox -------------------------------------

Public Sub initForm()

    On Error Resume Next
    
    Dim aSplitDefaultExportFields As Variant
    Dim sDefaultExportFieldsList As String

    LISTfields.Clear
        
    For i = 0 To FRMmain.LISTfields.ListCount - 1
    
        LISTfields.AddItem FRMmain.LISTfields.List(i)
        
    Next
    
    ' Select the default export fields -----------------------------------------------------------------------
     
    sDefaultExportFieldsList = getPref("", "defaultExportFields")
    
    aSplitDefaultExportFields = Split(sDefaultExportFieldsList, ",")
    
    For i = 0 To Me.LISTfields.ListCount
    
        For j = 0 To UBound(aSplitDefaultExportFields)
        
            If aSplitDefaultExportFields(j) = LISTfields.List(i) Then
            
                LISTfields.Selected(i) = True
                
                Exit For
            
            End If
        
        Next
        
    Next

    TXTexportPath.Text = getDefaultExportFilePath

    ProgressBar.Visible = False

    CMDExport.Enabled = True

End Sub

Private Function export() As Boolean

    Dim bExported As Boolean

' Validate file name and field selection, and call the export method in the export module ------------------------------------------

' Check that at least one field is selected in the listbox ------------------------------------------------------------------------------

    If LISTfields.SelCount < 1 Then
    
        MsgBox "Please Choose at Least One Field To Export"
        
        Exit Function
    
    End If
    
    ' Check for a file name ------------------------------------------------------------------------------------------
    
    If TXTexportPath.Text = "" Then
    
        MsgBox "Please Type A Name For The File"
        TXTexportPath.SetFocus
        
        Exit Function
        
    End If
    
    Dim sFieldsToExport As String

    If Trim(LISTfields.List(LISTfields.ListIndex)) = "All" Then
    
        sFieldsToExport = "!All!"
        
    Else
        
        For i = 0 To LISTfields.ListCount - 1
        
            If LISTfields.Selected(i) = True Then
            
                sFieldsToExport = sFieldsToExport & "[" & LISTfields.List(i) & "],"
                
            End If
            
        Next
        
        ' Remove the last 'comma' --------------------------------------------------------------------------------------
        
        sFieldsToExport = Left(sFieldsToExport, Len(sFieldsToExport) - 1)
        
    End If
    
    If RADIOexportToExcel.Value = True Then
    
        ' Export to Excel file ----------------------------------------------------------------------------------------------------
    
        bExported = MODexport.exportToExcel(FRMmain.LISTtables.List(FRMmain.LISTtables.ListIndex), sFieldsToExport, TXTexportPath.Text)
        
    Else
    
        ' Export to text file -------------------------------------------------------------------------------------------------------
    
        bExported = MODexport.exportToText(FRMmain.LISTtables.List(FRMmain.LISTtables.ListIndex), sFieldsToExport, TXTexportPath.Text)
        
    End If
    
    export = bExported
    
End Function


' Build a path for the file to be exported -------------------------------------------------------------------------------------------------------

Private Function getDefaultExportFilePath()

    On Error Resume Next

    Dim _
        sFileName As String

    sFileName = FRMmain.LISTtables.List(FRMmain.LISTtables.ListIndex)
    
    sFilePath = App.Path & "\" & sFileName & "_" & Day(Now) & "_" & Month(Now) & ".txt"
    
    getDefaultExportFilePath = sFilePath

End Function

' When user clicks browse to choose a path for exporting -----------------------------------------------------------------------------------

Private Sub CMDbrowse_Click()
    
    On Error GoTo errorHandler
    
    With FRMmain.CommonDialog
         
        .CancelError = True
        
        .FileName = getDefaultExportFilePath
        
        .Filter = "Text Documents (*.txt)|*.txt|Excel (*.xls)|*.xls|All Files|*.*"
        
        .ShowSave
        
        TXTexportPath.Text = .FileName
    
    End With
            
    Exit Sub
        
errorHandler:

    Select Case Err.Number
    
        Case 32755 ' Cancel was selected

            Exit Sub
        
        Case 20477 ' Invalid file name
        
            MsgBox "The path """ & sFilePath & """ is invalid"
            
            Exit Sub
        
    End Select

End Sub

' Select all items when user clicks 'All' -------------------------------------------------------------------------------------------------

Private Sub LISTfields_Click()

    On Error Resume Next
    
    If LISTfields.Selected(0) = True Then
    
        For i = 1 To LISTfields.ListCount - 1
        
            LISTfields.Selected(i) = True
            
        Next
        
    End If
    
End Sub

' Export the database ---------------------------------------------------------------------------------------------------

Private Sub CMDExport_Click()

    On Error Resume Next
    
    If export Then Me.Hide
    
End Sub

Private Sub RADIOexportToText_Click()
    
    On Error Resume Next
    
    If LCase(Right(TXTexportPath.Text, 4)) = ".xls" Then
    
        TXTexportPath.Text = Left(TXTexportPath.Text, Len(TXTexportPath.Text) - 4) & ".txt"
        
    Else
    
        TXTexportPath.Text = TXTexportPath.Text & ".txt"
        
    End If
    
End Sub
Private Sub RADIOexportToExcel_Click()
    
    On Error Resume Next
    
    If LCase(Right(TXTexportPath.Text, 4)) = ".txt" Then
    
        TXTexportPath.Text = Left(TXTexportPath.Text, Len(TXTexportPath.Text) - 4) & ".xls"
        
    Else
    
        TXTexportPath.Text = TXTexportPath.Text & ".xls"
        
    End If
    
End Sub

Private Sub CMDcancel_Click()
    
    Unload Me

End Sub
